package cn.edu.cug.cs.gtl.apps.ui;


import cn.edu.cug.cs.gtl.apis.ui.datake.catalogtree.CatalogLoginDialog;
import cn.edu.cug.cs.gtl.apis.ui.datake.catalogtree.CatalogPanel;
import cn.edu.cug.cs.gtl.apis.ui.datake.devicetree.DevicePanel;
import cn.edu.cug.cs.gtl.apis.ui.spacex.SpaceXFrame;
import cn.edu.cug.cs.gtl.apps.ui.actions.AppExitActionListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Locale;
import java.util.ResourceBundle;

public class MainWindow extends SpaceXFrame {

    JSplitPane splitPane = null;
    JTabbedPane leftTabbedPane = null;
    JTabbedPane rightTabbedPane = null;


    JMenu fileMenu = new JMenu("File");
    JMenu editMenu = new JMenu("Edit");
    JMenu dataMenu = new JMenu("Data");
    JMenu modellingMenu = new JMenu("Modelling");
    JMenu analysisMenu = new JMenu("Analysis");
    JMenu viewMenu = new JMenu("View");
    JMenu toolMenu = new JMenu("Tool");
    JMenuItem pluginMenuItem = new JMenuItem("Plugin...");
    JMenu helpMenu = new JMenu("Help");

    JToolBar fileToolBar = new JToolBar("File");
    JToolBar editToolBar = new JToolBar("Edit");
    JToolBar modellingToolBar = new JToolBar("Modelling");
    JToolBar analysisToolBar = new JToolBar("Analysis");
    JToolBar viewToolBar = new JToolBar("View");
    JToolBar toolToolBar = new JToolBar("Tool");
    JToolBar helpToolBar = new JToolBar("Help");


    public MainWindow(MainApp theApp) throws HeadlessException {
        this.theApp = theApp;
    }

    public MainWindow(MainApp theApp, GraphicsConfiguration gc) {
        super(gc);
        this.theApp = theApp;
    }

    public MainWindow(MainApp theApp, String title) throws HeadlessException {
        super(title);
        this.theApp = theApp;
    }

    public MainWindow(MainApp theApp, String title, GraphicsConfiguration gc) {
        super(title, gc);
        this.theApp = theApp;
    }


    @Override
    public JMenu getFileMenu() {
        return fileMenu;
    }

    @Override
    public JMenu getEditMenu() {
        return editMenu;
    }
    public JMenu getDataMenu() {
        return dataMenu;
    }

    @Override
    public JMenu getViewMenu() {
        return viewMenu;
    }

    @Override
    public JMenu getToolMenu() {
        return toolMenu;
    }

    @Override
    public JMenu getHelpMenu() {
        return helpMenu;
    }

    @Override
    public JToolBar getFileToolBar() {
        return fileToolBar;
    }

    @Override
    public JToolBar getViewToolBar() {
        return viewToolBar;
    }

    @Override
    public JToolBar getEditToolBar() {
        return editToolBar;
    }

    @Override
    public JToolBar getToolToolBar() {
        return toolToolBar;
    }

    @Override
    public JToolBar getHelpToolBar() {
        return helpToolBar;
    }

    @Override
    public JSplitPane getSplitPane() {
        return this.splitPane;
    }

    @Override
    public JTabbedPane getLeftTabbedPane() {
        return this.leftTabbedPane;
    }

    @Override
    public JTabbedPane getRightTabbedPane() {
        return this.rightTabbedPane;
    }

    @Override
    public JMenu getModellingMenu() {
        return modellingMenu;
    }

    @Override
    public JMenu getAnalysisMenu() {
        return analysisMenu;
    }

    @Override
    public JToolBar getModellingToolBar() {
        return modellingToolBar;
    }

    @Override
    public JToolBar getAnalysisToolBar() {
        return analysisToolBar;
    }

    public void initial() {
        initialMenus();
//        initialRenderPanel();
//        initialProjectPanel();
//        initialTablePanel();
        initialDevicePanel();
        initialCatalogPanel();
        initialLeftTabbedPane();
        initialRightTabbedPane();

        getContentPane().setLayout(new BorderLayout());
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftTabbedPane, rightTabbedPane);
        splitPane.setEnabled(true);
        splitPane.setDividerLocation(300);

        getContentPane().add(splitPane, BorderLayout.CENTER);
        initialTooBars();

        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    private void initialLeftTabbedPane() {
        leftTabbedPane = new JTabbedPane();
        ResourceBundle resourceBundle = ResourceBundle.getBundle("app_resource", Locale.getDefault());
        leftTabbedPane.addTab(resourceBundle.getString("data.catalog"), this.catalogPanel);
        leftTabbedPane.add(resourceBundle.getString("data.device"), this.devicePanel);
        leftTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        leftTabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
        leftTabbedPane.setSelectedIndex(0);

        leftTabbedPane.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int tabIndex = leftTabbedPane.indexAtLocation(e.getX(), e.getY());
                    if (tabIndex == 0) {
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
                                CatalogLoginDialog catalogLoginDialog = new CatalogLoginDialog(theApp);
//                            }
//                        });
                    }
                }
            }
        });
    }


    private void initialRightTabbedPane() {
        rightTabbedPane = new JTabbedPane();
        ResourceBundle resourceBundle = ResourceBundle.getBundle("app_resource", Locale.getDefault());

        JComponent model3dAppUI = getSpaceXApp()
                .getModel3dUIApp().initializeUI();
        JComponent tableAppUI = getSpaceXApp().getTableUIApp().initializeUI();
        JComponent map2dAppUI = getSpaceXApp().getMapUIApp().initializeUI();
        JComponent seriesAppUI = getSpaceXApp().getSeriesUIApp().initializeUI();
        JComponent textAppUI = getSpaceXApp().getTextUIApp().initializeUI();
        JComponent imageAppUI = getSpaceXApp().getImageUIApp().initializeUI();

        rightTabbedPane.addTab(resourceBundle.getString("data.model3d"), model3dAppUI);
        rightTabbedPane.addTab(resourceBundle.getString("data.map2d"), map2dAppUI);
        rightTabbedPane.addTab(resourceBundle.getString("data.image"), imageAppUI);
        rightTabbedPane.addTab(resourceBundle.getString("data.series"), seriesAppUI);
        rightTabbedPane.addTab(resourceBundle.getString("data.relation"), tableAppUI);
        rightTabbedPane.addTab(resourceBundle.getString("data.text"), textAppUI);

        rightTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        rightTabbedPane.setTabPlacement(JTabbedPane.TOP);
        rightTabbedPane.setSelectedIndex(0);
    }

    private void initialDevicePanel() {
        devicePanel = new DevicePanel(theApp);
        devicePanel.initial();
    }

    private void initialCatalogPanel() {
        catalogPanel = new CatalogPanel(theApp);
        catalogPanel.initial();
    }

    private void initialMenus() {
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);


        ResourceBundle resourceBundle = ResourceBundle.getBundle("app_resource", Locale.getDefault());

        JMenuBar menubar = new JMenuBar();

        fileMenu.setText(resourceBundle.getString("menu.file"));
        menubar.add(fileMenu);

        JMenuItem appExitItem = new JMenuItem(resourceBundle.getString("menu.file.exit"));
        appExitItem.addActionListener(new AppExitActionListener(theApp));
        fileMenu.add(appExitItem);

        editMenu.setText(resourceBundle.getString("menu.edit"));
        menubar.add(editMenu);

        dataMenu.setText(resourceBundle.getString("menu.data"));
        menubar.add(dataMenu);

        modellingMenu.setText(resourceBundle.getString("menu.modelling"));
        menubar.add(modellingMenu);

        analysisMenu.setText(resourceBundle.getString("menu.analysis"));
        menubar.add(analysisMenu);

        viewMenu.setText(resourceBundle.getString("menu.view"));
        menubar.add(viewMenu);

        toolMenu.setText(resourceBundle.getString("menu.tool"));
        menubar.add(toolMenu);

        pluginMenuItem.setText(resourceBundle.getString("menu.tool.plugin"));
        toolMenu.add(pluginMenuItem);

        helpMenu.setText(resourceBundle.getString("menu.help"));
        menubar.add(helpMenu);

        this.setJMenuBar(menubar);
    }

    private void initialTooBars() {
        JPanel toobarPanel = new JPanel(new FlowLayout(0));
        toobarPanel.add(fileToolBar, FlowLayout.LEFT);
        toobarPanel.add(editToolBar, FlowLayout.LEFT);
        toobarPanel.add(modellingToolBar, FlowLayout.LEFT);
        toobarPanel.add(analysisToolBar, FlowLayout.LEFT);
        toobarPanel.add(viewToolBar, FlowLayout.LEFT);
        toobarPanel.add(toolToolBar, FlowLayout.LEFT);
        toobarPanel.add(helpToolBar, FlowLayout.LEFT);

        getContentPane().add(toobarPanel, BorderLayout.NORTH);
    }


}
